package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationadaptersdk.mnsapif028v2.service.MnSapIf028V2Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MessageHeaderSDO;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf028Request;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.epoch.app.otsapplicationadaptersdk.service.SapService;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 中台同步SAP创建交货单 - 创建
 * 中台 -> sap
 * @author baizhuo
 * @version 1.0
 * @date 2023/12/29 19:24
 */
@Component
@Primary
public class MnSapIf028V2ServiceImpl implements MnSapIf028V2Service {

    private final Log log = Log.getLogger(MnSapIf028V2ServiceImpl.class);

    @Resource
    private SapService sapService;

    @Override
    @ProcessInvoker(remark = "MnSapIf028V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnSapCommResponse createDeliveryOrder(MnSapIf028Request mnSapIf028Request) {
        log.info("MnSapIf028V2ServiceImpl createDeliveryOrder param:{}", JSON.toJSONString(mnSapIf028Request));
        mnSapIf028Request.setHeader(generateSapMessageHeaderSDD());
        log.info("MnSapIf028V2ServiceImpl restSapDeliveryOrderCreate param:{}", JSON.toJSONString(mnSapIf028Request));
        Object result = sapService.restSapDeliveryOrderCreate(mnSapIf028Request);
//        Object result = null;
        log.info("MnSapIf028V2ServiceImpl createDeliveryOrder result:{}", JSON.toJSONString(result));
        MnSapCommResponse mnSapCommResponse = new MnSapCommResponse();
        MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
        mnSapCommSDO.setStatus(MnSAPStatus.SUCCESS.getCode());
        mnSapCommSDO.setMessage(MnSAPStatus.SUCCESS.getMsg());
        mnSapCommResponse.setData(Lists.newArrayList(mnSapCommSDO));
        return mnSapCommResponse;
    }

    private MessageHeaderSDO generateSapMessageHeaderSDD() {
        String messageId = UUID.randomUUID().toString();
        MessageHeaderSDO messageHeader = new MessageHeaderSDO();
        messageHeader.setMessageId(messageId.replaceAll("-", ""));
        messageHeader.setInterfacePath("/RESTAdapter/ZT/ECC/JHD_Creat");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messageHeader.setSendTime(dateFormat.format(new Date()));
        messageHeader.setSender("YWZT");
        messageHeader.setReceiver("ECC");
        return messageHeader;
    }

}
